﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Drawing.Printing;
using VSSAHT;
using VSS_AHT.Properties;
using VSSAHT.Transfer;
using System.IO;

namespace VSS_AHT
{
    class Common
    {
        internal static UserDTO UserLogin;
        internal static SerialPort Port;

        internal static string Template = @"Template\Ticket.dot";
        internal static string AdminAccount = "admin";

        internal static string defaultUsername = "nimda";
        internal static string defaultPassword = "092";
        internal static string passManualInput = "AHT";

        internal static string logfile = "log.txt";
        internal static string workstation = Settings.Default.VehicleStation; //"P1";

        //Duong dan luu tru phieu can

        internal static string storagePath = Settings.Default.storagePath; //@"E:\";
        internal static string RememberPath = Path.GetTempPath() + "remember_aht.vss";

        internal static byte StartByte = Settings.Default.StartByte;//G
        internal static byte EndByte = Settings.Default.EndByte;//k
        internal static int SkipBytes=Settings.Default.SkipBytes;

        private static System.Net.IPAddress _ipAddress = null;
        /// <summary>
        /// IPAddress
        /// </summary>
        public static System.Net.IPAddress IPAddress
        {
            get
            {
                if (_ipAddress == null)
                {
                    System.Net.IPHostEntry host = System.Net.Dns.GetHostByName(System.Windows.Forms.SystemInformation.ComputerName);
                    _ipAddress = host.AddressList[0];
                }
                return _ipAddress;
            }
        }
        //  internal static string RememberPath = Path.GetTempPath() + "remeberfile.vss";


        #region MessageBox
        internal static bool? ShowQuestion(string message, params object[] param)
        {
            DialogResult result = MessageBox.Show(String.Format(message, param), "Thông báo", MessageBoxButtons.YesNoCancel,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);

            if (result == DialogResult.Yes) { return true; }
            else if (result == DialogResult.No) { return false; }

            return null;
        }

        internal static bool ShowYesNoQuestion(string message, params object[] param)
        {
            DialogResult result = MessageBox.Show(String.Format(message, param), "Thông báo", MessageBoxButtons.YesNo,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

            return result == DialogResult.Yes;
        }

        internal static bool ShowOKCancelQuestion(string message, params object[] param)
        {
            DialogResult result = MessageBox.Show(String.Format(message, param), "Thông báo", MessageBoxButtons.OKCancel,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);

            return result == DialogResult.Yes;
        }

        internal static void ShowAlert(string message, params object[] param)
        {
            DialogResult result = MessageBox.Show(String.Format(message, param), "Thông báo", MessageBoxButtons.OK,
                MessageBoxIcon.Error);
        }

        internal static void ShowSuccessful(string message, params object[] param)
        {
            DialogResult result = MessageBox.Show(String.Format(message, param), "Thông báo", MessageBoxButtons.OK,
                MessageBoxIcon.Information);
        }
        #endregion

        internal static void InitPort()
        {
            try
            {
                ParaPort paraPort = new ParaPort();
                if (Port == null)
                    Port = new System.IO.Ports.SerialPort();

                if (!Port.IsOpen)
                {
                    // Config ports.
                    Port.PortName = paraPort.PortName;
                    Port.BaudRate = (int)paraPort.BaudRate;
                    Port.Parity = (System.IO.Ports.Parity)paraPort.Parity;
                    Port.DataBits = (int)paraPort.DataBits;
                    Port.StopBits = (System.IO.Ports.StopBits)paraPort.StopBits;
                }

                
                StartByte = paraPort.StartByte;
                EndByte = paraPort.EndByte;
                SkipBytes = paraPort.SkipBytes;
            }
            catch (Exception ex)
            {
                ShowAlert(Resources.NotConnectPort + ex.Message);
            }
        }

        public static bool CheckPrinter(string printerName)
        {
            foreach (var item in PrinterSettings.InstalledPrinters)
            {
                if (item.Equals(printerName))
                    return true;
            }
            return false;
        }
    }
}
